package fightplan.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JFileChooser;

import fightplan.business.Tournament;
import fightplan.persistence.ExcelImport;
import fightplan.views.MainView;

@SuppressWarnings("serial")
public class ImportExcelAction extends UndoableAction {

	private MainView mainView;

	public ImportExcelAction(MainView mainView) {
		super("Teilnehmer importieren", mainView, "Teilnehmer aus Excelliste importieren");
		this.mainView = mainView;
	}

	public boolean doAction(ActionEvent actionEvent) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setMultiSelectionEnabled(true);
		fileChooser.setCurrentDirectory(new File("."));
		fileChooser.setDialogTitle("Excel - Liste auswählen");
		fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
			public boolean accept(File f) {
				return !f.isFile() || f.getName().endsWith(".xls");
			}

			public String getDescription() {
				return "*.xls (Excelfile)";
			}
		});
		if (fileChooser.showDialog(mainView, "Importieren") == JFileChooser.APPROVE_OPTION) {
			Tournament tournament = (Tournament) mainView.getTournament().getValue();
			boolean success = ExcelImport.imprtFightersAndTeams(tournament, fileChooser.getSelectedFiles());
			if (success) {
				mainView.getTournament().fireChange();
			}
			return success;
		}
		return false;
	}

}